package com.yuncheng.oaApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.entity.OaProcessBusiness;
import com.yuncheng.oaApi.entity.OaJobEntity;
import com.yuncheng.oaApi.entity.OaSysTzMessage;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.oaApi.mapper.OaSysTzMessageMapper;
import com.yuncheng.oaApi.service.IOaProcessBusinessService;
import com.yuncheng.oaApi.service.IOaSpcySqsxJobService;
import com.yuncheng.oaApi.service.IOaSysTzMessageService;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import groovy.lang.Lazy;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import utils.DateConversionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 用户通知信息表
 * @Author: yuncheng
 * @Date: 2023-09-22
 * @Version: V1.0
 */
@Service
public class OaSysTzMessageServiceImpl extends BaseServiceImpl<OaSysTzMessageMapper, OaSysTzMessage> implements IOaSysTzMessageService {
    private static final Logger log = LoggerFactory.getLogger(OaSysTzMessageServiceImpl.class);

    @Resource
    @Lazy
    private IOaSpcySqsxJobService spcySqsxJobService;

    @Resource
    @Lazy
    private IOaProcessBusinessService oaProcessBusinessService;

    @Resource
    @Lazy
    private OaSysTzMessageMapper oaSysTzMessageMapper;

    //事项信息提交/发起通知 事项主id
    @Override
    public void tjMessage(String sxid){
        endMessage(sxid,false,false,false);
    }

    /**
     *
     * @param sxid
     * /事项结束后通知给发起人 业务表fuserid
     * @param sfxh 如果为 false 直接跳出循环 只循环一次
     * @param sfjs 如果为 true 表示流程结束
     * @param sfth 如果为 true 表示 是退回
     */
    public void endMessage(String sxid , boolean sfxh, boolean sfjs, boolean sfth){
        List<OaSysTzMessage> message = new ArrayList<>();

        List<OaProcessBusiness> oaProcessBusinessList = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>()
                .eq(OaProcessBusiness::getFpid, sxid));
        if (CollectionUtil.isEmpty(oaProcessBusinessList)){
            return;
        }

        String flcmb = oaProcessBusinessList.get(0).getFlcmb();
        LambdaQueryWrapper<OaJobEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OaJobEntity::getFpid, sxid);
        if (!sfjs){ //false
            queryWrapper.eq(OaJobEntity::getFblzt, OaCommonFlowConstant.BLZT_DB);
        }

        //如果结束则会查出所有的流程记录 需要拿到第一条流程的办理人 设置为接收人
        if (sfjs){
            queryWrapper.isNull(OaJobEntity::getFsyhjid);
        }

        if (sfth){
            queryWrapper.eq(OaJobEntity::getFblzt, OaCommonFlowConstant.BLZT_TH);
        }

        List<OaJobEntity> jobList = spcySqsxJobService.list(queryWrapper);

        if (CollectionUtil.isEmpty(jobList)){
            return;
        }


        for (OaJobEntity setMessage : jobList){
            if (StringUtils.isBlank(setMessage.getFdbrid())){
                log.error("待办人参数为空！流程id参数：{}", setMessage.getId());
                continue;
            }

            List<OaSysTzMessage> tzMessageList = oaSysTzMessageMapper.selectList(new LambdaQueryWrapper<OaSysTzMessage>().eq(OaSysTzMessage::getFsxid, sxid)
                                                                    .eq(OaSysTzMessage::getFjobid,setMessage.getId()));
            //如果已经查询pid的通知讯息则进行更新
            if(CollectionUtil.isNotEmpty(tzMessageList)){
                OaSysTzMessage messageServiceById = oaSysTzMessageMapper.selectById(tzMessageList.get(0).getId());

                oaSysTzMessageMapper.updateById(messageServiceById);
            }



            List<String> dbrIdsList = Arrays.asList(setMessage.getFdbrid().split(",|，"));
            for (String dbr : dbrIdsList){
                OaSysTzMessage oaSysTzMessage = new OaSysTzMessage();
                oaSysTzMessage.setId(GetUuIdUtils.ReplaceUuId());
                oaSysTzMessage.setFjsuserid(dbr);
                oaSysTzMessage.setFdqsxsfbj(OaCommonFlowConstant.getLczt(sfjs));
                oaSysTzMessage.setFtzsxlx(OaCommonFlowConstant.TZ_SXMC);
                oaSysTzMessage.setFsxwcsj(setMessage.getFfssj());
                oaSysTzMessage.setFsxmc(setMessage.getFlcmc());
                oaSysTzMessage.setFsxid(setMessage.getFpid());
                oaSysTzMessage.setFjobid(setMessage.getId());
                oaSysTzMessage.setFsfyd(OaCommonFlowConstant.TZ_ZTWCK);
                oaSysTzMessage.setFzbhj(setMessage.getFhjmc());
                oaSysTzMessage.setFlcmc(setMessage.getFlcmc());
                oaSysTzMessage.setFlcbm(flcmb);
                if (sfjs){
                    oaSysTzMessage.setFmessage("您申请的[" + setMessage.getFlcmc() + "]" + "在[" + DateConversionUtils.DateToYMDHMSChineseString(new Date()) + "]" + "已办结。");
                    log.info("更新事项 完结 信息完成");

                }else{
                    oaSysTzMessage.setFmessage("您有["+setMessage.getFlcmc()+"]" +"在["+setMessage.getFhjmc()+"]环节待办。");
                    log.info("更新事项 发起/提交 信息完成");
                }
                if (sfth){
                    oaSysTzMessage.setFmessage("您申请的[" + setMessage.getFlcmc() + "]" + "在[" + DateConversionUtils.DateToYMDHMSChineseString(new Date()) + "]被退回，如有疑问请联系人事。");
                    log.info("更新事项 退回 信息完成");
                }
                message.add(oaSysTzMessage);
                break;
            }
        }

        if (CollectionUtil.isNotEmpty(message)){
            insertMessageList(message);
        }
    }

    @Transactional(rollbackFor = BusinessException.class)
    public void insertMessageList(List<OaSysTzMessage> message){
        boolean b = saveBatch(message);
        if (!b){
          throw new BusinessException("日志插入失败");
        }
    }
}
